<?php

/**
 * This is the model class for table "{{friends}}".
 *
 * The followings are the available columns in table '{{friends}}':
 * @property integer $id
 * @property integer $user_id
 * @property integer $friend_id
 * @property integer $status
 */
class Friends extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Friends the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{friends}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('user_id, friend_id, status', 'required'),
			array('user_id, friend_id, status', 'numerical', 'integerOnly'=>true),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, user_id, friend_id, status', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'friend_info'=> array(self::BELONGS_TO, 'Users', 'friend_id'),
			
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'user_id' => 'User',
			'friend_id' => 'Friend',
			'status' => 'Status',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('user_id',$this->user_id);
		$criteria->compare('friend_id',$this->friend_id);
		$criteria->compare('status',$this->status);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
	
	public function searchFriends($data){
		$connection=Yii::app()->db;
		$sql='SELECT f.id as friendship_id,
					 f.friend_id, 
					 u.*,
					 p.name as myavatar
				FROM {{friends}} AS f
		   LEFT JOIN {{users}} as u ';
		
		if(!empty($data['friend_id'])){
			$sql.='  ON f.user_id = u.id ';
		}else{
			$sql.='  ON f.friend_id = u.id ';
		}
		
		$sql.=' LEFT JOIN {{photos}} AS p
					   ON p.id=u.avatar
				    WHERE f.status = "' . $data['status'] . '" ';
		
		if(!empty($data['filter_name'])){
			$sql .=	' AND u.full_name like "%' . $data['filter_name'] . '%" ';
		}	

		if(!empty($data['filter_message'])){
			$sql .=	' AND u.get_message != 0 ';
		}		
	
		if(!empty($data['user_id'])){
			$sql .=	' AND f.user_id = "' . $data['user_id'] . '" ';
		}		

		if(!empty($data['friend_id'])){
			$sql .=	' AND f.friend_id = "' . $data['friend_id'] . '" ';
		}		
		
		if(!empty($data['filter_age_to'])){
			$sql .=	' AND u.birthday >= DATE_SUB(curdate(), INTERVAL ' . $data['filter_age_to'] . ' YEAR) ';
		}
		
		if(!empty($data['filter_age_from'])){
			$sql .=	' AND u.birthday <= DATE_SUB(curdate(), INTERVAL ' . $data['filter_age_from'] . ' YEAR) ';
		}		
		
		if(!empty($data['sex'])){
			$sql .=	' AND u.sex = "' . $data['sex'] . '" ';
		}
		
		if(!empty($data['country_id'])){
			$sql .=	' AND u.country_id = "' . $data['country_id'] . '" ';
		}		
		
		if(!empty($data['filter_online'])){
			$sql .=	' AND u.last_visit > "' . date("Y-m-d H:i:s", time() - 15*60) . '" ';
		}	
		
		$sql .=	'ORDER BY u.full_name ASC';
		
		if(!empty($data['limit'])){
			if(!empty($data['offset'])){
				$sql .=	' LIMIT ' . $data['offset'] . ',' . $data['limit'];
			}else{
				$sql .=	' LIMIT ' . $data['limit'];
			}
		}		

		return $connection->createCommand($sql)->queryAll();		
	}
	
	public function countFriends($data){
		$connection=Yii::app()->db;
		$sql='SELECT count(friend_id) as total
				FROM {{friends}} as f
		   LEFT JOIN {{users}} as u ';
		
		if(!empty($data['friend_id'])){
			$sql.='  ON f.user_id = u.id ';
		}else{
			$sql.='  ON f.friend_id = u.id ';
		}			
		
		$sql.=' LEFT JOIN {{photos}} AS p
					   ON p.id=u.avatar
				    WHERE f.status = "' . $data['status'] . '" ';

		if(!empty($data['user_id'])){
			$sql .=	' AND f.user_id = "' . $data['user_id'] . '" ';
		}
		
		if(!empty($data['filter_message'])){
			$sql .=	' AND u.get_message != 0 ';
		}		
		
		if(!empty($data['friend_id'])){
			$sql .=	' AND f.friend_id = "' . $data['friend_id'] . '" ';
		}		

		if(!empty($data['filter_online'])){
			$sql .=	' AND u.last_visit > "' . date("Y-m-d H:i:s", time() - 15*60) . '" ';
		}			

		return $connection->createCommand($sql)->queryRow();
	}
}